package com.zyb.educenter.controller;


import com.zyb.commonutils.JwtUtils;
import com.zyb.commonutils.R;
import com.zyb.commonutils.comment.UcenterMemberComment;
import com.zyb.commonutils.ordervo.UcenterMemberOrder;
import com.zyb.educenter.entity.UcenterMember;
import com.zyb.educenter.entity.vo.RegisterVo;
import com.zyb.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testJava
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/educenter/ucenter-member")
//@CrossOrigin
public class UcenterMemberController {

    @Resource
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    //注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember memberInfo = memberService.getById(memberIdByJwtToken);
        return R.ok().data("memberInfo", memberInfo);
    }

    //根据用户ID获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        UcenterMemberOrder memberOrder=new UcenterMemberOrder();
        //把member对象里面值复制给UcenterMemberOrder对象
        BeanUtils.copyProperties(member,memberOrder);
        return memberOrder;
    }

    //根据token字符串获取用户信息
    @PostMapping("getUserInfo/{userId}")
    public UcenterMemberComment getUserInfo(@PathVariable String userId){
        UcenterMember ucenterMember = memberService.getById(userId);

        UcenterMemberComment memberComment=new UcenterMemberComment();
        BeanUtils.copyProperties(ucenterMember,memberComment);
        return memberComment;
    }

    //根据日期统计当天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count=memberService.countRegisterDay(day);
        return R.ok().data("count",count);
    }

}

